package me.sdau.spider.kafkaprocess.kafka;

import lombok.extern.slf4j.Slf4j;
import me.sdau.spider.kafkaprocess.service.ElasticSearchService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author zj
 * @date 2018/3/29 10:35
 */
@Slf4j
@Component
public class Receiver {

    @Autowired
    ElasticSearchService elasticSearchService;

    @KafkaListener(topics = "testII", containerFactory = "kafkaListenerContainerFactory")
    public void processMessage(List<ConsumerRecord<String, String>> records, Acknowledgment ack) {
        System.out.println(ack);
//        records.forEach(record -> {
//            elasticSearchService.consumData(record.key(), record.value());
//        });

        elasticSearchService.batchConsum(records);

        // 手动提交偏移量
        ack.acknowledge();

    }
}
